package com.bite.friend.controller.user;


import com.bite.api.domain.vo.UserQuestionResultVO;
import com.bite.common.core.constants.HttpConstants;
import com.bite.common.core.controller.BaseController;
import com.bite.common.core.domain.R;
import com.bite.common.core.domain.vo.LoginUserVO;
import com.bite.friend.aspect.CheckUserStatus;
import com.bite.friend.domain.user.dto.UserDTO;
import com.bite.friend.domain.user.dto.UserEditDTO;
import com.bite.friend.domain.user.vo.UserVO;
import com.bite.friend.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/user")
@RestController
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

    @PostMapping("/sendCode")
    public R<Void> sendCode(@RequestBody UserDTO userDTO) {
        return toR(userService.sendCode(userDTO));
    }

    @PostMapping("/code/login")
    public R<String> codeLogin(@RequestBody UserDTO userDTO) {
        return ok(userService.codeLogin(userDTO));
    }

    @DeleteMapping("/logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        token = transToken(token);
        return toR(userService.logout(token)); // 通过BaseController 中的方法
    }

    // 获取登录信息
    @GetMapping("/info")
    public R<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        token = transToken(token);
        return ok(userService.info(token));
    }

    @GetMapping("/detail")
    public R<UserVO> detail() {
        return ok(userService.detail());
    }

    @PutMapping("/edit")
    public R<Void> edit(@RequestBody @Validated UserEditDTO userEditDTO) {
        return toR(userService.edit(userEditDTO));
    }

    @CheckUserStatus
    @PutMapping("/avatar/update")
    public R<Void> avatarUpdate(@RequestBody UserEditDTO userEditDTO) {
        return toR(userService.avatarUpdate(userEditDTO));
    }
}
